package com.talent.web;

import com.talent.common.Const;
import com.talent.page.Page;
import com.talent.pojo.Notice;
import com.talent.pojo.User;
import com.talent.service.NoticeService;
import com.talent.vo.NoticeUserVo;
import com.talent.vo.Result;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/manage/notice")
public class NoticeController extends BaseController {

    @Autowired
    NoticeService noticeService;

    @RequestMapping("/manager")
    public ModelAndView manager(ModelAndView mav, NoticeUserVo noticeUserVo, Integer currentPage){
        Page<NoticeUserVo> page = noticeService.selectNoticeUserVoPage(noticeUserVo, currentPage == null ? 1: currentPage);
        mav.addObject("page", page.getResultList());
        initPageParams(mav, page);
        mav.addObject("noticeUserVo", noticeUserVo);
        mav.setViewName("/notice/manager");
        return mav;
    }

    /**
     * @Title: addUI
     * @Description: 去添加页面
     * @return
     */

    @RequestMapping("addUI")
    public ModelAndView noticeAddUI() {
        ModelAndView mav = new ModelAndView();
        try {
            mav.setViewName("/notice/addUI");
        } catch (Exception e) {
            e.printStackTrace();
            mav.setViewName("common/error");
            mav.addObject("error", "有异常！");
        }
        return mav;
    }

    @RequestMapping("add")
    @ResponseBody
    public Result noticeAdd(Notice notice) {
        try {
            User user = (User) getSession().getAttribute(Const.CURRENT_USER);
            notice.setCreator(user.getId());
            Date day=new Date();    
            notice.setTime(day);
            noticeService.addNotice(notice);
            return new Result(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }
    
    /**
     * 批量删除
     * @return
     */
    @RequestMapping("noticeDeleteIDs")
    @ResponseBody
    public Result deleteIDs(String ids) {
        try {
            noticeService.deleteAll(ids.split(","));
            return new Result(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("noticeDelete")
    @ResponseBody
    public Result classDelete(Integer id) {
        try {
            noticeService.deleteById(id);
            return new Result(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }


    /**
     * 编辑
     * @param id
     * @return
     */
    @RequestMapping("noticeUpdateUI")
    public ModelAndView noticeUpdateUI(Integer id) {
        ModelAndView mav = new ModelAndView();
        try {
            NoticeUserVo notice = noticeService.selectNoticeUserVoById(id);
            mav.addObject("notice", notice);
            mav.setViewName("notice/editUI");
        } catch (Exception e) {
            e.printStackTrace();
            mav.setViewName("common/error");
            mav.addObject("error", "有异常！");
        }
        return mav;
    }

    @RequestMapping("noticeUpdate")
    @ResponseBody
    public Result classUpdate(NoticeUserVo noticeUserVo) {

        try {
            User user = (User) getSession().getAttribute(Const.CURRENT_USER);
            int flag = noticeService.editNoticeUserVo(noticeUserVo, user.getId());
            if(flag == 0){
                return new Result(false,"不是本人创建的公告不允许修改！");
            }
            return new Result(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }


    @RequestMapping("noticeDetail")
    public ModelAndView noticeDetail(Integer id) {
        ModelAndView mav = new ModelAndView();
        try {
            NoticeUserVo noticeUserVo = noticeService.selectNoticeUserVoById(id);
            mav.addObject("notice", noticeUserVo);
            mav.setViewName("notice/detail");
        } catch (Exception e) {
            e.printStackTrace();
            mav.setViewName("common/error");
            mav.addObject("error", "有异常！");
        }
        return mav;
    }
}
